package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.luck.picture.lib.config.PictureMimeType;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.FireTaskBean;

import java.util.List;


public class FirePictureVideoAdapter extends RecyclerView.Adapter<FirePictureVideoAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> mData;


    public FirePictureVideoAdapter(Context context, List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> data) {
        this.context = context;
        this.mData = data;
    }


    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_picture_video_voice, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        viewHolder.layoutFrame.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        holder.layoutFrame.setTag(position);
        FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean eventAttachEntitiesBean = mData.get(position);
        String attachUrl = eventAttachEntitiesBean.getUrl();
        boolean isUrlHasImage = PictureMimeType.isUrlHasImage(attachUrl);
        Glide.with(context)
                .load(attachUrl)
                .apply(BaseApplication.getRequestOptions())
                .into(holder.riv);
        if (isUrlHasImage) {
            holder.ivStartButton.setVisibility(View.GONE);
        }else{
            holder.ivStartButton.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void upData(List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final ShapeableImageView riv;
        private final ImageView ivStartButton;
        private final FrameLayout layoutFrame;
        private View convertView;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            riv = (ShapeableImageView) convertView.findViewById(com.mingtu.uploadevent.R.id.riv);
            ivStartButton = (ImageView) convertView.findViewById(com.mingtu.uploadevent.R.id.iv_start_button);
            layoutFrame = (FrameLayout) convertView.findViewById(com.mingtu.uploadevent.R.id.layout_frame);

            ViewGroup.LayoutParams lp = layoutFrame.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth();
            int mWidth = (screenWidth - SizeUtils.dp2px(20) * 2 - SizeUtils.dp2px(15) * 4) / 3;
            lp.width = mWidth;
            lp.height = mWidth;
            layoutFrame.setLayoutParams(lp);
        }
    }

}
